package com.wcs.exam.controller.admin;

import com.wcs.exam.common.base.Result;
import com.wcs.exam.common.base.annotation.OptLog;
import com.wcs.exam.common.base.annotation.OptLogCache;
import com.wcs.exam.common.base.page.Page;
import com.wcs.exam.common.config.ThreadContext;
import com.wcs.exam.controller.admin.biz.AdminSysUserBiz;
import com.wcs.exam.controller.admin.req.*;
import com.wcs.exam.controller.admin.resp.AdminSysUserPageResp;
import com.wcs.exam.controller.admin.resp.AdminSysUserViewResp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Admin-后台用户信息
 *
 * @author wcs
 * @since 2024-07-06
 */
@Tag(name = "Admin-后台用户信息")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/admin/sys/user")
public class AdminSysUserController {

    @NotNull
    private final AdminSysUserBiz biz;

    @Operation(summary = "后台用户信息-分页")
    @GetMapping(value = "/page")
    public Result<Page<AdminSysUserPageResp>> page(@ModelAttribute AdminSysUserPageReq req) {
        return biz.page(req);
    }

    @Operation(summary = "后台用户信息-添加")
    @OptLog(value = "后台用户信息添加")
    @PostMapping(value = "/save")
    public Result<String> save(@RequestBody @Valid AdminSysUserSaveReq req) {
        return biz.save(req);
    }

    @Operation(summary = "后台用户信息-查看")
    @Parameter(name = "id", description = "主键ID", required = true)
    @OptLogCache
    @GetMapping(value = "/view")
    public Result<AdminSysUserViewResp> view(@RequestParam Long id) {
        return biz.view(id);
    }

    @Operation(summary = "后台用户信息-编辑")
    @OptLog(value = "后台用户信息编辑", isUpdate = true)
    @PutMapping(value = "/edit")
    public Result<String> edit(@RequestBody @Valid AdminSysUserEditReq req) {
        return biz.edit(req);
    }

    @Operation(summary = "后台用户信息-编辑更新状态")
    @OptLog(value = "后台用户信息-编辑更新状态")
    @PutMapping(value = "/update/status")
    public Result<String> updateStatus(@RequestBody @Valid AdminSysUserUpdateStatusReq req) {
        return biz.updateStatus(req);
    }

    @Operation(summary = "后台用户信息-排序")
    @OptLog(value = "后台用户信息排序")
    @PutMapping(value = "/sort")
    public Result<String> sort(@RequestBody @Valid List<AdminSysUserSortReq> reqs) {
        return biz.sort(reqs);
    }

    @Operation(summary = "后台用户信息-删除")
    @Parameter(name = "id", description = "主键ID", required = true)
    @OptLog(value = "后台用户信息")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam Long id) {
        return biz.delete(id);
    }

    @Operation(summary = "后台用户信息-密码修改")
    @OptLog(value = "后台用户信息密码修改", isUpdate = true)
    @PutMapping(value = "/password")
    public Result<String> password(@RequestBody @Valid AdminSysUserPasswordReq req) {
        return biz.password(req);
    }

    @Operation(summary = "后台用户信息-当前登录用户")
    @GetMapping(value = "/current")
    public Result<AdminSysUserViewResp> current() {
        return biz.view(ThreadContext.getUserId());
    }
}
